/*
 * Copyright (c) 2015 DHC Corporation.
 */
package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.util.CommonUtils;

import static cn.com.dhc.danlu.dlware.global.DanluApplication.agent;

/**
 * 商品描述中ListView的适配器
 *
 * @author LiYongHe
 * @version 1.00 2015/03/05 新建
 */
public class GoodInfoInstructionListViewAdapter extends MyBaseAdapter<String> {

    public GoodInfoInstructionListViewAdapter (Context context) {
        super (context);
    }

    @Override
    protected View createView (int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = mInflater.inflate (R.layout.dl_gooddes_pic, parent, false);
            holder = new ViewHolder (convertView);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) holder.mSimpleDraweeView.getLayoutParams ();
            lp.width = CommonUtils.getScreenWidth (mInflater.getContext ());
            holder.mSimpleDraweeView.setLayoutParams (lp);
            GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder (mInflater.getContext ().getResources ());
            GenericDraweeHierarchy hierarchy = builder.setActualImageScaleType (ScalingUtils.ScaleType.FIT_XY).build ();
            holder.mSimpleDraweeView.setHierarchy (hierarchy);
            convertView.setTag (holder);
        } else {
            holder = (ViewHolder) convertView.getTag ();
        }
        String url = (String) getItem (position);
        if (url.contains ("@") && agent.isWifi ()) {
            url = url.substring (0, url.indexOf ("@"));
        }
        holder.mSimpleDraweeView.setImageURI (Uri.parse (url));
        return convertView;
    }

    private static class ViewHolder {
        SimpleDraweeView mSimpleDraweeView;

        public ViewHolder (View view) {
            mSimpleDraweeView = (SimpleDraweeView) view.findViewById (R.id.sdv_pic);
        }
    }
}